/*
 * Copyright 2008-2009 Daniel Cachapa <cachapa@gmail.com>
 * 
 * This program is distributed under the terms of the GNU General Public Licence Version 3
 * The licence can be read in its entirety in the LICENSE.txt file accompaning this source code,
 * or at: http://www.gnu.org/copyleft/gpl.html
 * 
 * This file is part of WeightWatch.
 *
 * WeightWatch is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * WeightWatch is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the WeightWatch source code. If not, see
 * <http://www.gnu.org/licenses/>.
 */

package net.cachapa.weightwatch;

import net.cachapa.weightwatch.util.Serialize;
import net.cachapa.weightwatch.util.MyDate;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class Plan implements Serialize {

	public static final int CALORIES_PER_KILOGRAM = 7716;
	private MyDate startDate;
	private double startWeight;
	private double goalWeight;
	private double weeklyChange;
	private int dailyBalance;
	private MyDate endDate;

	public Plan() {
		startDate = new MyDate();
		endDate = new MyDate();
	}

	public Plan(MyDate startDate, double startWeight, double goalWeight, int dailyBalance) {
		this.startDate = new MyDate(startDate);
		this.startWeight = startWeight;
		this.goalWeight = goalWeight;
		this.dailyBalance = dailyBalance;
		this.endDate = new MyDate();
		setDailyBalance(dailyBalance);
	}
	
	public Plan(Plan plan) {
		this.startDate = new MyDate(plan.getStartDate());
		this.startWeight = plan.startWeight;
		this.goalWeight = plan.goalWeight;
		this.weeklyChange = plan.weeklyChange;
		this.dailyBalance = plan.dailyBalance;
		this.endDate = new MyDate(plan.endDate);
	}

	public double getValueAt(MyDate date) {
		if (date.isBefore(this.startDate)) {
			return -1;
		}
		if (endDate.isBefore(date)) {
			return goalWeight;
		}
		int daysSinceStart = startDate.differenceInDays(date);
		return startWeight + dailyBalance * daysSinceStart / (double)CALORIES_PER_KILOGRAM;
	}

	public void setStartDate(MyDate date) {
		startDate = date;
		double totalWeightChange = startWeight - goalWeight;
		double weightChangePerDay = (double)dailyBalance / (double)CALORIES_PER_KILOGRAM;
		int durationInDays = (int)Math.abs(totalWeightChange / weightChangePerDay);
		endDate.setTime(startDate.getTime());
		endDate.addDays(durationInDays);
	}
	
	public MyDate getStartDate() {
		return startDate;
	}

	public void setStartWeight(double value) {
		startWeight = value;
		computeEndDate();
	}
	
	public double getStartWeight() {
		return startWeight;
	}

	public void setGoalWeight(double value) {
		goalWeight = value;
		computeEndDate();
	}

	public double getGoalWeight() {
		return goalWeight;
	}

	public void setWeeklyChange(double value) {
		weeklyChange = value;
		dailyBalance = (int)((weeklyChange / (double)7) * (double)CALORIES_PER_KILOGRAM);
		computeEndDate();
	}
	
	public double getWeeklyChange() {
		return weeklyChange;
	}

	public void setDailyBalance(int value) {
		dailyBalance = value;
		double weightChangePerDay = (double)dailyBalance / (double)Plan.CALORIES_PER_KILOGRAM;
		weeklyChange = (double)(int)((weightChangePerDay * 7) * 100) / 100;
		computeEndDate();
	}
	
	public int getDailyBalance() {
		return dailyBalance;
	}

	public void setEndDate(MyDate date) {
		endDate = date;
		int differenceInDays = startDate.differenceInDays(endDate);
		double totalWeightChange = startWeight - goalWeight;
		double weeklyWeightChange = totalWeightChange / (double)differenceInDays * 7;
		setWeeklyChange(weeklyWeightChange);
	}

	public MyDate getEndDate() {
		return endDate;
	}

	private void computeEndDate() {
		double totalWeightChange = startWeight - goalWeight;
		double weightChangePerDay = (double) dailyBalance / (double) CALORIES_PER_KILOGRAM;
		int durationInDays = (int) Math.abs(totalWeightChange / weightChangePerDay);
		endDate.setTime(startDate.getTime());
		endDate.addDays(durationInDays);
	}

	public byte[] toByteArray() {
		try {
			ByteArrayOutputStream bout = new ByteArrayOutputStream();
			DataOutputStream dout = new DataOutputStream(bout);

			dout.writeLong(startDate.getTime());
			dout.writeDouble(startWeight);
			dout.writeDouble(goalWeight);
			dout.writeInt(dailyBalance);
			dout.flush();

			return bout.toByteArray();
		} catch (IOException ex) {
			ex.printStackTrace();
			return null;
		}
	}

	public void fromByteArray(byte[] data) {
		try {
			ByteArrayInputStream bin = new ByteArrayInputStream(data);
			DataInputStream din = new DataInputStream(bin);

			startDate = new MyDate(din.readLong());
			startWeight = din.readDouble();
			goalWeight = din.readDouble();
			dailyBalance = din.readInt();
			setDailyBalance(dailyBalance);
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
}
